
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>检测算法 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-accordion/accordion.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="APP.html" />
    
    
    <link rel="prev" href="DATASET.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">目录</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../function/">
            
                <a href="../function/">
            
                    
                    产品功能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    开发技术
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="DATASET.html">
            
                <a href="DATASET.html">
            
                    
                    数据集
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.2" data-path="ALGORITHM.html">
            
                <a href="ALGORITHM.html">
            
                    
                    检测算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="APP.html">
            
                <a href="APP.html">
            
                    
                    App 端构建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="SERVER.html">
            
                <a href="SERVER.html">
            
                    
                    后端构建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >检测算法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x6DF1;&#x5EA6;&#x53EF;&#x5206;&#x79BB;&#x5377;&#x79EF;&#x68C0;&#x6D4B;&#x7B97;&#x6CD5;">&#x6DF1;&#x5EA6;&#x53EF;&#x5206;&#x79BB;&#x5377;&#x79EF;&#x68C0;&#x6D4B;&#x7B97;&#x6CD5;</h3>
<p>Language: Python</p>
<p>&#x4F7F;&#x7528;TensorFlow &#x6DF1;&#x5EA6;&#x5B66;&#x4E60;&#x6846;&#x67B6;&#xFF0C;&#x4F7F;&#x7528;Keras&#x4F1A;&#x5927;&#x5E45;&#x7F29;&#x51CF;&#x4EE3;&#x7801;&#x91CF;</p>
<p>&#x8BAD;&#x7EC3;&#x673A;&#x5668;&#xFF1A;&#x534E;&#x4E3A;Atlas 200 AI&#x5F00;&#x53D1;&#x677F;&#xFF08;&#x6216;&#x672C;&#x5730;&#x8BA1;&#x7B97;&#x673A;&#xFF09;</p>
<p><a href="../../../medicine-dataset">&#x6570;&#x636E;&#x96C6;</a></p>
<p>&#x5E38;&#x7528;&#x7684;<strong>&#x5377;&#x79EF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;</strong>&#x53CA;&#x5728;ImageNet&#x4E0A;&#x7684;&#x51C6;&#x786E;&#x7387;</p>
<table>
<thead>
<tr>
<th style="text-align:center">&#x6A21;&#x578B;</th>
<th style="text-align:center">&#x5927;&#x5C0F;</th>
<th style="text-align:center">Top-1&#x51C6;&#x786E;&#x7387;</th>
<th style="text-align:center">Top-5&#x51C6;&#x786E;&#x7387;</th>
<th style="text-align:center">&#x53C2;&#x6570;&#x6570;&#x91CF;</th>
<th style="text-align:center">&#x6DF1;&#x5EA6;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">Xception</td>
<td style="text-align:center">88 MB</td>
<td style="text-align:center">0.790</td>
<td style="text-align:center">0.945</td>
<td style="text-align:center">22,910,480</td>
<td style="text-align:center">126</td>
</tr>
<tr>
<td style="text-align:center">VGG16</td>
<td style="text-align:center">528 MB</td>
<td style="text-align:center">0.713</td>
<td style="text-align:center">0.901</td>
<td style="text-align:center">138,357,544</td>
<td style="text-align:center">23</td>
</tr>
<tr>
<td style="text-align:center">VGG19</td>
<td style="text-align:center">549 MB</td>
<td style="text-align:center">0.713</td>
<td style="text-align:center">0.900</td>
<td style="text-align:center">143,667,240</td>
<td style="text-align:center">26</td>
</tr>
<tr>
<td style="text-align:center">ResNet50</td>
<td style="text-align:center">98 MB</td>
<td style="text-align:center">0.749</td>
<td style="text-align:center">0.921</td>
<td style="text-align:center">25,636,712</td>
<td style="text-align:center">168</td>
</tr>
<tr>
<td style="text-align:center">ResNet101</td>
<td style="text-align:center">171 MB</td>
<td style="text-align:center">0.764</td>
<td style="text-align:center">0.928</td>
<td style="text-align:center">44,707,176</td>
<td style="text-align:center">-</td>
</tr>
<tr>
<td style="text-align:center">ResNet152</td>
<td style="text-align:center">232 MB</td>
<td style="text-align:center">0.766</td>
<td style="text-align:center">0.931</td>
<td style="text-align:center">60,419,944</td>
<td style="text-align:center">-</td>
</tr>
<tr>
<td style="text-align:center">ResNet50V2</td>
<td style="text-align:center">98 MB</td>
<td style="text-align:center">0.760</td>
<td style="text-align:center">0.930</td>
<td style="text-align:center">25,613,800</td>
<td style="text-align:center">-</td>
</tr>
<tr>
<td style="text-align:center">ResNet101V2</td>
<td style="text-align:center">171 MB</td>
<td style="text-align:center">0.772</td>
<td style="text-align:center">0.938</td>
<td style="text-align:center">44,675,560</td>
<td style="text-align:center">-</td>
</tr>
<tr>
<td style="text-align:center">ResNet152V2</td>
<td style="text-align:center">232 MB</td>
<td style="text-align:center">0.780</td>
<td style="text-align:center">0.942</td>
<td style="text-align:center">60,380,648</td>
<td style="text-align:center">-</td>
</tr>
<tr>
<td style="text-align:center">ResNeXt50</td>
<td style="text-align:center">96 MB</td>
<td style="text-align:center">0.777</td>
<td style="text-align:center">0.938</td>
<td style="text-align:center">25,097,128</td>
<td style="text-align:center">-</td>
</tr>
<tr>
<td style="text-align:center">ResNeXt101</td>
<td style="text-align:center">170 MB</td>
<td style="text-align:center">0.787</td>
<td style="text-align:center">0.943</td>
<td style="text-align:center">44,315,560</td>
<td style="text-align:center">-</td>
</tr>
<tr>
<td style="text-align:center">InceptionV3</td>
<td style="text-align:center">92 MB</td>
<td style="text-align:center">0.779</td>
<td style="text-align:center">0.937</td>
<td style="text-align:center">23,851,784</td>
<td style="text-align:center">159</td>
</tr>
<tr>
<td style="text-align:center">InceptionResNetV2</td>
<td style="text-align:center">215 MB</td>
<td style="text-align:center">0.803</td>
<td style="text-align:center">0.953</td>
<td style="text-align:center">55,873,736</td>
<td style="text-align:center">572</td>
</tr>
<tr>
<td style="text-align:center">MobileNet</td>
<td style="text-align:center">16 MB</td>
<td style="text-align:center">0.704</td>
<td style="text-align:center">0.895</td>
<td style="text-align:center">4,253,864</td>
<td style="text-align:center">88</td>
</tr>
<tr>
<td style="text-align:center">MobileNetV2</td>
<td style="text-align:center">14 MB</td>
<td style="text-align:center">0.713</td>
<td style="text-align:center">0.901</td>
<td style="text-align:center">3,538,984</td>
<td style="text-align:center">88</td>
</tr>
<tr>
<td style="text-align:center">DenseNet121</td>
<td style="text-align:center">33 MB</td>
<td style="text-align:center">0.750</td>
<td style="text-align:center">0.923</td>
<td style="text-align:center">8,062,504</td>
<td style="text-align:center">121</td>
</tr>
<tr>
<td style="text-align:center">DenseNet169</td>
<td style="text-align:center">57 MB</td>
<td style="text-align:center">0.762</td>
<td style="text-align:center">0.932</td>
<td style="text-align:center">14,307,880</td>
<td style="text-align:center">169</td>
</tr>
<tr>
<td style="text-align:center">DenseNet201</td>
<td style="text-align:center">80 MB</td>
<td style="text-align:center">0.773</td>
<td style="text-align:center">0.936</td>
<td style="text-align:center">20,242,984</td>
<td style="text-align:center">201</td>
</tr>
<tr>
<td style="text-align:center">NASNetMobile</td>
<td style="text-align:center">23 MB</td>
<td style="text-align:center">0.744</td>
<td style="text-align:center">0.919</td>
<td style="text-align:center">5,326,716</td>
<td style="text-align:center">-</td>
</tr>
<tr>
<td style="text-align:center">NASNetLarge</td>
<td style="text-align:center">343 MB</td>
<td style="text-align:center">0.825</td>
<td style="text-align:center">0.960</td>
<td style="text-align:center">88,949,818</td>
<td style="text-align:center">-</td>
</tr>
</tbody>
</table>
<p>&#x7531;&#x4E8E;&#x786C;&#x4EF6;&#x6761;&#x4EF6;&#x9650;&#x5236;&#xFF0C;&#x7EFC;&#x5408;&#x8003;&#x8651;&#x6A21;&#x578B;&#x7684;&#x51C6;&#x786E;&#x7387;&#x3001;&#x5927;&#x5C0F;&#x4EE5;&#x53CA;&#x590D;&#x6742;&#x5EA6;&#x7B49;&#x56E0;&#x7D20;&#xFF0C;&#x91C7;&#x7528;&#x4E86;<strong>Xception&#x6A21;&#x578B;</strong>&#xFF0C;
&#x8BE5;&#x6A21;&#x578B;&#x662F;134&#x5C42;&#xFF08;&#x5305;&#x542B;&#x6FC0;&#x6D3B;&#x5C42;&#xFF0C;&#x6279;&#x6807;&#x51C6;&#x5316;&#x5C42;&#x7B49;&#xFF09;&#x62D3;&#x6251;&#x6DF1;&#x5EA6;&#x7684;&#x5377;&#x79EF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x3002;</p>
<h2 id="&#x68C0;&#x6D4B;&#x7B97;&#x6CD5;">&#x68C0;&#x6D4B;&#x7B97;&#x6CD5;</h2>
<pre><code class="lang-python">def Xception(include_top=True,
    weights=&apos;imagenet&apos;,
    input_tensor=None,
    input_shape=None,
    pooling=None,
    classes=1000,
    **kwargs)

# &#x53C2;&#x6570;
# include_top&#xFF1A;&#x662F;&#x5426;&#x4FDD;&#x7559;&#x9876;&#x5C42;&#x7684;&#x5168;&#x8FDE;&#x63A5;&#x7F51;&#x7EDC;
# weights&#xFF1A;None&#x4EE3;&#x8868;&#x968F;&#x673A;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x5373;&#x4E0D;&#x52A0;&#x8F7D;&#x9884;&#x8BAD;&#x7EC3;&#x6743;&#x91CD;&#x3002;&apos;imagenet&#x2019;&#x4EE3;&#x8868;&#x52A0;&#x8F7D;&#x9884;&#x8BAD;&#x7EC3;&#x6743;&#x91CD;
# input_tensor&#xFF1A;&#x53EF;&#x586B;&#x5165;Keras tensor&#x4F5C;&#x4E3A;&#x6A21;&#x578B;&#x7684;&#x56FE;&#x50CF;&#x8F93;&#x5165;tensor
# input_shape&#xFF1A;&#x53EF;&#x9009;&#xFF0C;&#x4EC5;&#x5F53;include_top=False&#x6709;&#x6548;&#xFF0C;&#x5E94;&#x4E3A;&#x957F;&#x4E3A;3&#x7684;tuple&#xFF0C;&#x6307;&#x660E;&#x8F93;&#x5165;&#x56FE;&#x7247;&#x7684;shape&#xFF0C;&#x56FE;&#x7247;&#x7684;&#x5BBD;&#x9AD8;&#x5FC5;&#x987B;&#x5927;&#x4E8E;71&#xFF0C;&#x5982;(150,150,3)
# pooling&#xFF1A;&#x5F53;include_top=False&#x65F6;&#xFF0C;&#x8BE5;&#x53C2;&#x6570;&#x6307;&#x5B9A;&#x4E86;&#x6C60;&#x5316;&#x65B9;&#x5F0F;&#x3002;None&#x4EE3;&#x8868;&#x4E0D;&#x6C60;&#x5316;&#xFF0C;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5377;&#x79EF;&#x5C42;&#x7684;&#x8F93;&#x51FA;&#x4E3A;4D&#x5F20;&#x91CF;&#x3002;&#x2018;avg&#x2019;&#x4EE3;&#x8868;&#x5168;&#x5C40;&#x5E73;&#x5747;&#x6C60;&#x5316;&#xFF0C;&#x2018;max&#x2019;&#x4EE3;&#x8868;&#x5168;&#x5C40;&#x6700;&#x5927;&#x503C;&#x6C60;&#x5316;&#x3002;
# classes&#xFF1A;&#x53EF;&#x9009;&#xFF0C;&#x56FE;&#x7247;&#x5206;&#x7C7B;&#x7684;&#x7C7B;&#x522B;&#x6570;&#xFF0C;&#x4EC5;&#x5F53;include_top=True&#x5E76;&#x4E14;&#x4E0D;&#x52A0;&#x8F7D;&#x9884;&#x8BAD;&#x7EC3;&#x6743;&#x91CD;&#x65F6;&#x53EF;&#x7528;
</code></pre>
<p><a href="../../../medicine-model/src">&#x57FA;&#x4E8E;Xception&#x7684;&#x6A21;&#x578B;&#x5FAE;&#x8C03;,&#x8BE6;&#x7EC6;&#x8BF7;&#x53C2;&#x8003;&#x4EE3;&#x7801;</a></p>
<ol>
<li><p>&#x8BBE;&#x7F6E;Xception&#x53C2;&#x6570;</p>
<p> &#x8FC1;&#x79FB;&#x5B66;&#x4E60;&#x53C2;&#x6570;&#x6743;&#x91CD;&#x52A0;&#x8F7D;&#xFF1A;xception_weights</p>
<pre><code class="lang-python"> <span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x8F93;&#x5165;&#x56FE;&#x50CF;&#x7684;&#x5BBD;&#x9AD8;&#x4EE5;&#x53CA;&#x901A;&#x9053;&#x6570;</span>
 img_size = (<span class="hljs-number">299</span>, <span class="hljs-number">299</span>, <span class="hljs-number">3</span>)

 base_model = keras.applications.xception.Xception(include_top=<span class="hljs-keyword">False</span>,
                                                 weights=<span class="hljs-string">&apos;..\\resources\\keras-model\\xception_weights_tf_dim_ordering_tf_kernels_notop.h5&apos;</span>,
                                                 input_shape=img_size,
                                                 pooling=<span class="hljs-string">&apos;avg&apos;</span>)

 <span class="hljs-comment"># &#x5168;&#x8FDE;&#x63A5;&#x5C42;&#xFF0C;&#x4F7F;&#x7528;softmax&#x6FC0;&#x6D3B;&#x51FD;&#x6570;&#x8BA1;&#x7B97;&#x6982;&#x7387;&#x503C;&#xFF0C;&#x5206;&#x7C7B;&#x5927;&#x5C0F;&#x662F;628</span>
 model = keras.layers.Dense(<span class="hljs-number">628</span>, activation=<span class="hljs-string">&apos;softmax&apos;</span>, name=<span class="hljs-string">&apos;predictions&apos;</span>)(base_model.output)
 model = keras.Model(base_model.input, model)

 <span class="hljs-comment"># &#x9501;&#x5B9A;&#x5377;&#x79EF;&#x5C42;</span>
 <span class="hljs-keyword">for</span> layer <span class="hljs-keyword">in</span> base_model.layers:
   layer.trainable = <span class="hljs-keyword">False</span>
</code></pre>
</li>
<li><p>&#x5168;&#x8FDE;&#x63A5;&#x5C42;&#x8BAD;&#x7EC3;(v1.0)</p>
<pre><code class="lang-python"> <span class="hljs-keyword">from</span> base_model <span class="hljs-keyword">import</span> model

 <span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x8BAD;&#x7EC3;&#x96C6;&#x56FE;&#x7247;&#x5927;&#x5C0F;&#x4EE5;&#x53CA;&#x76EE;&#x5F55;&#x53C2;&#x6570;</span>
 img_size = (<span class="hljs-number">299</span>, <span class="hljs-number">299</span>)
 dataset_dir = <span class="hljs-string">&apos;..\\dataset\\dataset&apos;</span>
 img_save_to_dir = <span class="hljs-string">&apos;resources\\image-traing\\&apos;</span>
 log_dir = <span class="hljs-string">&apos;resources\\train-log&apos;</span>

 model_dir = <span class="hljs-string">&apos;resources\\keras-model\\&apos;</span>

 <span class="hljs-comment"># &#x4F7F;&#x7528;&#x6570;&#x636E;&#x589E;&#x5F3A;</span>
 train_datagen = keras.preprocessing.image.ImageDataGenerator(
     rescale=<span class="hljs-number">1.</span> / <span class="hljs-number">255</span>,
     shear_range=<span class="hljs-number">0.2</span>,
     width_shift_range=<span class="hljs-number">0.4</span>,
     height_shift_range=<span class="hljs-number">0.4</span>,
     rotation_range=<span class="hljs-number">90</span>,
     zoom_range=<span class="hljs-number">0.7</span>,
     horizontal_flip=<span class="hljs-keyword">True</span>,
     vertical_flip=<span class="hljs-keyword">True</span>,
     preprocessing_function=keras.applications.xception.preprocess_input)

 test_datagen = keras.preprocessing.image.ImageDataGenerator(
     preprocessing_function=keras.applications.xception.preprocess_input)

 train_generator = train_datagen.flow_from_directory(
     dataset_dir,
     save_to_dir=img_save_to_dir,
     target_size=img_size,
     class_mode=<span class="hljs-string">&apos;categorical&apos;</span>)

 validation_generator = test_datagen.flow_from_directory(
     dataset_dir,
     save_to_dir=img_save_to_dir,
     target_size=img_size,
     class_mode=<span class="hljs-string">&apos;categorical&apos;</span>)

 <span class="hljs-comment"># &#x65E9;&#x505C;&#x6CD5;&#x4EE5;&#x53CA;&#x52A8;&#x6001;&#x5B66;&#x4E60;&#x7387;&#x8BBE;&#x7F6E;</span>
 early_stop = EarlyStopping(monitor=<span class="hljs-string">&apos;val_loss&apos;</span>, patience=<span class="hljs-number">13</span>)
 reduce_lr = ReduceLROnPlateau(monitor=<span class="hljs-string">&apos;val_loss&apos;</span>, patience=<span class="hljs-number">7</span>, mode=<span class="hljs-string">&apos;auto&apos;</span>, factor=<span class="hljs-number">0.2</span>)
 tensorboard = keras.callbacks.tensorboard_v2.TensorBoard(log_dir=log_dir)

 <span class="hljs-keyword">for</span> layer <span class="hljs-keyword">in</span> model.layers:
     layer.trainable = <span class="hljs-keyword">False</span>

 <span class="hljs-comment"># &#x6A21;&#x578B;&#x7F16;&#x8BD1;</span>
 model.compile(optimizer=<span class="hljs-string">&apos;rmsprop&apos;</span>, loss=<span class="hljs-string">&apos;categorical_crossentropy&apos;</span>, metrics=[<span class="hljs-string">&apos;accuracy&apos;</span>])

 history = model.fit_generator(train_generator,
                               steps_per_epoch=train_generator.samples // train_generator.batch_size,
                               epochs=<span class="hljs-number">100</span>,
                               validation_data=validation_generator,
                               validation_steps=validation_generator.samples // validation_generator.batch_size,
                               callbacks=[early_stop, reduce_lr, tensorboard])
 <span class="hljs-comment"># &#x6A21;&#x578B;&#x5BFC;&#x51FA;</span>
 model.save(model_dir + <span class="hljs-string">&apos;chinese_medicine_model_v1.0.h5&apos;</span>)
</code></pre>
</li>
<li><p>&#x5BF9;&#x4E8E;&#x9876;&#x90E8;&#x7684;6&#x5C42;&#x5377;&#x79EF;&#x5C42;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x96C6;&#x5BF9;&#x6743;&#x91CD;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x5FAE;&#x8C03;</p>
<pre><code class="lang-python"> <span class="hljs-comment"># &#x52A0;&#x8F7D;&#x6A21;&#x578B;</span>
 model=keras.models.load_model(<span class="hljs-string">&apos;resources\\keras-model\\chinese_medicine_model_v2.0.h5&apos;</span>)

 <span class="hljs-keyword">for</span> layer <span class="hljs-keyword">in</span> model.layers:
    layer.trainable = <span class="hljs-keyword">False</span>
 <span class="hljs-keyword">for</span> layer <span class="hljs-keyword">in</span> model.layers[<span class="hljs-number">126</span>:<span class="hljs-number">132</span>]:
    layer.trainable = <span class="hljs-keyword">True</span>

 history = model.fit_generator(train_generator,
                               steps_per_epoch=train_generator.samples // train_generator.batch_size,
                               epochs=<span class="hljs-number">100</span>,
                               validation_data=validation_generator,
                               validation_steps=validation_generator.samples // validation_generator.batch_size,
                               callbacks=[early_stop, reduce_lr, tensorboard])
 model.save(model_dir + <span class="hljs-string">&apos;chinese_medicine_model_v2.0.h5&apos;</span>)
</code></pre>
</li>
<li><p>&#x5728;&#x540E;&#x7AEF;&#x9879;&#x76EE;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;Deeplearn4j&#x8C03;&#x7528;&#x8BAD;&#x7EC3;&#x597D;&#x7684;&#x6A21;&#x578B;
```
  public class CnnModelUtil {</p>
<pre><code> private static ComputationGraph CNN_MODEL = null;

 /**
  * &#x4E2D;&#x836F;&#x540D;&#x5B57;&#x7684;&#x7F16;&#x7801;
  */
 private static final Map&lt;Integer, String&gt; MEDICINE_NAME_MAP = new HashMap&lt;&gt;();

 /**
  * &#x5B9A;&#x4E49;cnn model&#x7684;&#x6587;&#x4EF6;&#x5939;&#x8DEF;&#x5F84;
  */
 private static final String DATA_DIR = System.getProperty(&quot;os.name&quot;)
         .toLowerCase().contains(&quot;windows&quot;) ? &quot;D:\\data\\model\\&quot;
         : &quot;./data/model/&quot;;

 /**
  * &#x5B9A;&#x4E49;&#x4E2D;&#x836F;&#x7F16;&#x7801;&#x8868;&#x7684;&#x6587;&#x4EF6;&#x540D;
  */
 private static final String MEDICINE_LABLE_FILE_NAME = &quot;medicine_name-lable.txt&quot;;

 /**
  * &#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7684;&#x6587;&#x4EF6;&#x540D;
  */
 private static final String CNN_MODEL_FILE_NAME = &quot;chinese_medicine_model.h5&quot;;
</code></pre></li>
</ol>
<pre><code>    /**
     * &#x56FE;&#x7247;&#x7684;&#x52A0;&#x8F7D;&#x5668;
     */
    private static final NativeImageLoader IMAGE_LOADER = new NativeImageLoader(299, 299, 3);


    /**
     * &#x521D;&#x59CB;&#x5316;
     */
    static {
        try {
            CNN_MODEL = KerasModelImport.importKerasModelAndWeights(DATA_DIR + CNN_MODEL_FILE_NAME);

            Files.readAllLines(Paths.get(DATA_DIR, MEDICINE_LABLE_FILE_NAME)).forEach(v -&gt; {
                String[] split = v.split(&quot;,&quot;);
                MEDICINE_NAME_MAP.put(Integer.valueOf(split[1]), split[0]);
            });
        } catch (IOException | InvalidKerasConfigurationException | UnsupportedKerasConfigurationException e) {
            e.printStackTrace();
        }
    }

    /**
     * &#x5BF9;&#x56FE;&#x50CF;&#x8FDB;&#x884C;&#x9884;&#x6D4B;
     * &#x5BF9;&#x9884;&#x6D4B;&#x7684;&#x6982;&#x7387;&#x503C;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x5904;&#x7406;
     * &#x8FD4;&#x56DE;&#x503C;&#x662F;&#x6982;&#x7387;&#x503C;&#x524D;10&#x7684;&#x4E2D;&#x836F;&#x7684;&#x540D;&#x5B57;
     * @param file
     * @return
     * @throws 
     */
    public static Map&lt;String, Float&gt; medicineNamePredict(File file) throws IOException {
        INDArray image = IMAGE_LOADER.asMatrix(file).divi(127.5).subi(1);
        INDArray output = CNN_MODEL.outputSingle(image);
        Map&lt;Integer, Float&gt; resultMap = new HashMap&lt;&gt;();
        float[] floats = output.toFloatVector();
        for (int i = 0; i &lt; floats.length; i++) {
            resultMap.put(i, floats[i]);
        }
        List&lt;Map.Entry&lt;Integer, Float&gt;&gt; resultList = new LinkedList&lt;&gt;(resultMap.entrySet());
        resultList.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));
        Map&lt;String, Float&gt; medicinePredict = new LinkedHashMap&lt;&gt;();
        resultList.stream().limit(10).forEach(v -&gt; {
            medicinePredict.put(MEDICINE_NAME_MAP.get(v.getKey()), v.getValue());
        });
        return medicinePredict;
    }
}
```
</code></pre><h3 id="&#x6A21;&#x578B;&#x6982;&#x89C8;">&#x6A21;&#x578B;&#x6982;&#x89C8;</h3>
<p><a href="../../assets/images/model.png">&#x6A21;&#x578B;&#x8BE6;&#x7EC6;&#x7ED3;&#x6784;</a></p>
<p><strong>&#x8BAD;&#x7EC3;&#x8FC7;&#x7A0B;&#x6B63;&#x786E;&#x7387;&#x4EE5;&#x53CA;&#x635F;&#x5931;&#x51FD;&#x6570;&#x53EF;&#x89C6;&#x5316;&#x5C55;&#x793A;</strong></p>
<p><img src="../../assets/images/&#x6B63;&#x786E;&#x7387;.png" alt="&#x6B63;&#x786E;&#x7387;">
<img src="../../assets/images/&#x635F;&#x5931;&#x51FD;&#x6570;.png" alt="&#x635F;&#x5931;&#x51FD;&#x6570;"></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="DATASET.html" class="navigation navigation-prev " aria-label="Previous page: 数据集">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="APP.html" class="navigation navigation-next " aria-label="Next page: App 端构建">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"检测算法","level":"1.3.2","depth":2,"next":{"title":"App 端构建","level":"1.3.3","depth":2,"path":"docs/technology/APP.md","ref":"docs/technology/APP.md","articles":[]},"previous":{"title":"数据集","level":"1.3.1","depth":2,"path":"docs/technology/DATASET.md","ref":"docs/technology/DATASET.md","articles":[]},"dir":"ltr"},"config":{"plugins":["splitter","copy-code-button","highlight","accordion","back-to-top-button"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"splitter":{},"accordion":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"back-to-top-button":{},"copy-code-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","links":{},"gitbook":"*"},"file":{"path":"docs/technology/ALGORITHM.md","mtime":"2021-04-02T04:05:01.247Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-04-02T08:17:55.461Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-accordion/accordionSelector.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

